// pages/activedetial/activedetial.js
var WxParse = require("../../wxParse/wxParse.js");
var api = require("../../api.js"),
  app = getApp();
var r;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    istrue: false,
    currentTab: 1,
    artDetail: {},
    artUser: {},
    consultes: [],
    type: [],
    remark: "",
    replyUserName: "",
    pageIndex: 2,
    hiddenmodalput: true,
    popup: true,
    loadMoreJson: false,
    loadMore: false,
    isFocus: true, //聚焦
    Value: "", //输入的内容
    popup1: true,
  },
  /**
   * 生命周期函数--监听页面加载
   *
   */
  onLoad: function(options) {
    r = this;
    /** 调用是否已授权的方法 */
    getApp().is_empower();
    //  console.log(options)
    var id = options.id;
    this.setData({
      id: id,
      sys: app.globalData.systeminfo
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var _this = this;
    r.toast = _this.selectComponent("#toast");
    // if (getApp().globalData.wxUserInfo.openId) {
    var userid = getApp().globalData.wxUserInfo.openId;
    //console.log(userid)
    wx.showLoading({
      title: "数据加载中"
    });
    var t = new Object();
    t.artId = _this.data.id
    t.openId = userid
    app.getData.artDetail(t).then(res => {
      setTimeout(function() {
        wx.hideLoading();
        const img = res.data.artDesc;
        res.data.artDesc = img.replace(/\<img/gi, '<img style="max-width:100%;height:auto;" ')
        _this.setData({
          artDetail: res.data,
          isCollection: res.data.isCollection,
          userid: userid
        });
        // WxParse.wxParse("artDesc","html", res.data.artDesc,_this,5);
      }, 500);

    }).catch(res => {
      wx.hideLoading();
      _this.setData({
        artDetail: [],
      });
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.showNavigationBarLoading(),
      console.log("下拉刷新"),
      this.onShow(),
      setTimeout(function() {
        wx.hideNavigationBarLoading(), wx.stopPullDownRefresh();
      }, 1500);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      title: r.data.artDetail.artTitle,
      path: `pages/index/index?id=${r.data.artDetail.artId}&shareType=1`,
      success: function(res) {},
      fail: function(res) {

      }
    }
  },

  // 切换导航
  switchNav: function(e) {
    var type = e.currentTarget.dataset.current;
    /** 作者信息 */
    if (type == 2) {
      var t = new Object();
      (t.id = r.data.artDetail.artCreatuser);
      wx.request({
        url: api.art.partner,
        data: t,
        success: function(res) {
          console.log(res.data);
          setTimeout(function() {
            wx.hideLoading();
            const img = res.data.data.partnerDesc;
            res.data.data.partnerDesc = img.replace(/\<img/gi, '<img style="max-width:100%;height:auto;" ')
            r.setData({
              artUser: res.data.data,
            });
          }, 1000);
        }
      });
    } else if (type == 3) {
      // 获取评论列表
      var t = new Object();
      (t.pageIndex = "1"), (t.pageSize = "3"), (t.otherId = r.data.id);
      wx.request({
        url: api.detail.consultData,
        data: t,
        success: function(res) {
          if (res.statusCode == 200 && res.data.code == 200) {
            if (res.data.data.length > 0) {
              r.setData({
                consultes: res.data.data,
                loadMoreJson: true,
                loadMore: true
              });
              // WxParse.wxParse("couDescription","html", res.data.data.couDescription,_this,5);
            } else {
              r.setData({
                consultes: [],
                loadMoreJson: false,
                loadMore: false
              });
            }
          }
        }
      });
    }
    r.setData({
      currentTab: type
    });

  },
  //提交评论
  replaySubmit: function(e) {
    var form = e.detail.value;
    var that = this;
    var user = getApp().globalData.wxUserInfo;
    var t = new Object();
    let conArea = form.conArea;
    if (conArea == "") {
      wx: wx.showToast({
        title: "内容为空"
      });
      return false;
    }
    (t.openId = user.openId), (t.otherId = r.data.id), (t.conArea = conArea), (t.type = 1);
    wx.request({
      url: api.detail.addConsult,
      data: t,
      method: "POST",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function(res) {
        console.log(res);
        if (res.statusCode == 200 && res.data.code == 200) {
          wx.showToast({
            title: "评论成功"
          });
          that.setData({
            popup: true
          });
          // 获取评论列表
          var t = new Object();
          var replayarr = [];
          (t.pageIndex = "1"), (t.pageSize = "3"), (t.otherId = that.data.id);
          wx.request({
            url: api.detail.consultData,
            data: t,
            success: function(res) {
              if (res.statusCode == 200 && res.data.code == 200) {
                if (res.data.data.length > 0) {
                  that.setData({
                    consultes: res.data.data,
                    // replayarr: replayarr
                  });
                  console.log(that.data);
                } else {
                  that.setData({
                    consultes: res.data.data,
                  });
                }
              }
            }
          });
        } else {
          wx.showToast({
            title: res.data.msg
          });
        }
      }
    });
  },
  // 下拉加载咨询
  loadMore: function() {
    var t = new Object();
    var that = this;
    var replayarr = [];
    t.pageIndex = that.data.pageIndex;
    t.pageSize = "3";
    t.otherId = that.data.id;
    if (!that.data.loadMoreJson) {
      return;
    }
    wx.request({
      url: api.detail.consultData,
      data: t,

      success: function(res) {
        if (res.statusCode == 200 && res.data.code == 200) {
          if (res.data.data.length > 0) {
            that.data.pageIndex++;
            that.setData({
              loadMoreJson: true,
              consultes: that.data.consultes.concat(res.data.data)
            });
            console.log(that.data);
          } else {
            that.setData({
              loadMoreJson: false
            });
          }
        }
      }
    });
  },
  onRemarkInput(event) {
    //保存输入框填写内容
    var value = event.detail.value;
    this.setData({
      remark: value
    });
  },

  bindReply: function(e) {
    const conId = e.currentTarget.dataset.id;
    this.setData({
      reply: true,
      popup1: false,
      conId: conId
    });
  },
  /* 隐藏弹窗 */
  hidePopup(flag = true) {
    console.log(flag);
    this.setData({
      popup: flag
    });
  },
  modalinput: function() {
    this.hidePopup(false);
  },
  //取消按钮
  cancel: function() {
    this.setData({
      hiddenmodalput: true
    });
  },
  //确认
  confirm: function() {
    this.setData({
      hiddenmodalput: true
    });
  },
  Focus(e) {
    var that = this;
    console.log(e.detail.value);
    var inputValue = e.detail.value;
    that.setData({
      Value: inputValue
    });
  },
  Tap() {
    var that = this;
    that.setData({
      isFocus: true
    });
  },
  //添加回复
  submitForm1(e) {
    console.log(e)
    var form = e.detail.value;
    var that = this;
    var t = new Object();
    let comment = form.comment;
    if (comment == "") {
      wx.showToast({
        title: "内容为空"
      });
      return false;
    }
    (t.conId = that.data.conId),
    (t.repArea = comment),
    wx.request({
      url: api.detail.addReply,
      data: t,
      method: "POST",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function(res) {
        console.log(res);
        if (res.statusCode == 200 && res.data.code == 200) {
          wx.showToast({
            title: "回复成功"
          });
          that.getConsultData();
          that.setData({
            popup: true
          });

        } else {
          wx.showToast({
            title: res.data.msg
          });
        }
      }
    });
  },
  //输出评价
  delReplay(e) {
    const conId = e.currentTarget.dataset.id;
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定要删除当前评论',
      success: function(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          var t = new Object();
          t.conId = conId;
          wx.request({
            url: api.detail.delConsult,
            data: t,
            method: "POST",
            header: {
              "content-type": "application/x-www-form-urlencoded"
            },
            success: function(res) {
              if (res.statusCode == 200) {
                if (res.data.code == 200) {
                  r.toast.showToast('删除成功');
                  that.getConsultData();
                  that.setData({
                    popup: true
                  });
                }
              }
            }
          })
        } else if (res.cancle) {
          onsole.log('用户点击取消')
        }

      }

    })
  },
  cancleReply: function() {
    this.setData({
      reply: false,
      popup1: true,
      conId: ''
    });
  },
  addCollection: function(e) {
    console.log(e)
    var that = this;
    const opeType = e.currentTarget.dataset.collection;
    const json = that.data.artDetail;
    const n = {
      openId: getApp().globalData.wxUserInfo.openId,
      otherId: json.artId,
      type: 1,
      opeType: opeType
    };
    if (opeType == 0) {
      n.img = json.inImg;
      n.title = json.artTitle;
    }
    wx.request({
      url: api.detail.add_del_Collection,
      data: n,
      method: "POST",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function(res) {
        if (res.statusCode == 200) {
          if (res.data.code == 200) {
            const tips = opeType == 0 ? '已收藏' : '已取消';
            r.setData({
              isCollection: opeType == 0 ? 1 : 0
            })
            r.toast.showToast(tips);
          }

        }
      }
    });
  },



});